import request from '@/utils/request'
import type { Response, PageDataList } from '@/utils/type'
import type { AxiosResponse } from 'axios'
import type { addGreenFullParams } from '@/api/back-management/data-management/interface'

export interface queryParams {
  regionIds: [] //区县id
  year?: string //年份
  state?: string //状态
  startTime?: string
  endTime?: string
  pageSize: number //分页数量
  pageIndex: number //页数
}
/**
 * 绿色完整社区报送
 *
 * @param {addGreenFullParams} params 新增参数
 */
export const addGreenCommunity = (params: addGreenFullParams) => {
  return request('/residential/green/community/add', {
    method: 'POST',
    data: params,
  }) as Promise<Response<any>>
}

/**
 * 编辑绿色完整社区报送
 *
 * @param {addGreenFullParams} params 更新参数
 */
export const updateGreenCommunity = (params: addGreenFullParams) => {
  return request('/residential/green/community/update', {
    method: 'POST',
    data: params,
  }) as Promise<Response<any>>
}

/**
 * 删除绿色完整社区报送
 *
 * @param  pkid
 */
export const deleteGreenCommunity = (pkid: string): Promise<Response<any>> => {
  return request(`/residential/green/community/delete/${pkid}`, {
    method: 'DELETE',
  })
}

/**
 * 查询绿色完整社区报送
 *
 * @param  pkid
 */
export const getGreenCommunity = (
  pkid: string
): Promise<Response<addGreenFullParams>> => {
  return request(`/residential/green/community/get/${pkid}`, {
    method: 'GET',
  })
}
/**
 * 分页绿色完整社区报送
 *
 * @param {queryParams} params 查询参数
 */
export const pageGreenCommunity = (params: queryParams) => {
  return request('/residential/green/community/query', {
    method: 'POST',
    data: params,
  }) as Promise<Response<any>>
}
/**
 * 社区总数查询
 *
 * @param  regionId 区县id
 */
export const getByCommunityNum = (
  regionId: string
): Promise<Response<number>> => {
  return request(`/residential/green/community/getByCommunityNum/${regionId}`, {
    method: 'GET',
  })
}
/**
 * 查询绿色名单或完整社区名单的社区个数
 *
 *  @param  regionId 区县id
 *  @param type 类型（0 绿色社区，1完整社区）
 *  @param year 年份
 */
export const getGreenCommunityNumber = (
  regionId: string,
  type: string,
  year: string
): Promise<Response<number>> => {
  return request(`/residential/green/community/getNumber`, {
    method: 'POST',
    params: {
      regionId: regionId,
      type: type,
      year: year,
    },
  })
}

/**
 * 查询绿色完整社区名单选项
 *
 *  @param  regionId 区县id
 *  @param year 年份
 */
export const getGreenCommunityByTable = (
  regionId: string,
  year: string
): Promise<Response<addGreenFullParams>> => {
  return request(`/residential/green/community/getByTable/${regionId}`, {
    method: 'GET',
  })
}
/**
 * 查询绿色完整社区名单选项
 *
 *  @param  regionId 区县id
 *  @param year 年份
 */
export const getGreenCommunityByUpdateTable = (
  regionId: any
): Promise<Response<addGreenFullParams>> => {
  return request(`/residential/green/community/getByUpdateTable/${regionId}`, {
    method: 'GET',
  })
}
/**
 * 查询社区得分
 *
 *  @param  communityId 社区id
 */
export const queryCommunityNum = (
  communityId: string
): Promise<Response<any>> => {
  return request(`/residential/synthesis/getBySum?communityId=${communityId}`)
}
/**
 * 查询待审核社区
 *
 *  @param  regionId 社区id
 */
export const queryByAudit = (regionId: string): Promise<Response<any>> => {
  return request(`/residential/green/community/getByAudit/${regionId}`)
}

/**
 * 查询未填报区县
 *
 */
export const queryUnfilled = (): Promise<Response<any>> => {
  return request(`/residential/green/community/selectNotFilled`)
}

/**
 * 查询未填报社区
 *
 */
export const queryUnfilledCommunity = (): Promise<Response<any>> => {
  return request(`/residential/basic/getUnfilled`)
}

/**
 * 审核绿色完整社区名单
 *
 *  @param  pkid 主键id
 *  @param  state 状态
 *  @param  reviewComments 审核理由（可不填）
 */
export const greenCommunityProcess = (
  pkid: string,
  state: string,
  reviewComments: string
): Promise<Response<addGreenFullParams>> => {
  return request(
    `/residential/green/community/process/${pkid}/${state}/${reviewComments}`,
    {
      method: 'GET',
    }
  )
}

export type QueryGreenListParams = {
  communityIds?: string[] //社区id
  streetIds?: string[] //街道id
  regionIds?: string[] //区id
  year?: string | number //评定年份
  demonstrate?: string //市级示范绿色完整社区
  startTime?: string
  endTime?: string
  pageSize: number //页面大小
  pageIndex: number //页数
}
export type ExportGreenListParams = {
  communityIds?: string[] //社区id
  streetIds?: string[] //街道id
  regionIds?: string[] //区id
  year?: string | number //评定年份
  demonstrate?: string //市级示范绿色完整社区
}

/**
 * 绿色社区名单
 *
 * @param data 查询参数
 */
export const queryGreenCommunityList = (
  data: QueryGreenListParams
): Promise<Response<PageDataList<any>>> => {
  return request(`/residential/green/roll/query`, {
    method: 'POST',
    data: data,
  })
}
/**
 * 绿色社区名单
 *
 * @param data 查询参数
 */
export const exportGreenCommunityList = (
  data: QueryGreenListParams
): Promise<Response<PageDataList<any>>> => {
  return request(`/residential/green/roll/exportExcel`, {
    method: 'POST',
    responseType: 'blob',
    data: data,
  })
}

export type QueryFullListParams = QueryGreenListParams & {
  pilot: string //住建部完整社区建设试点社区
}
export type ExportFullListParams = ExportGreenListParams & {
  pilot: string //住建部完整社区建设试点社区
}
/**
 * 完整社区名单
 *
 * @param data
 */
export const queryFullCommunityList = (
  data: QueryFullListParams
): Promise<Response<PageDataList<any>>> => {
  return request(`/residential/green/residential/query`, {
    method: 'POST',
    data,
  })
}
/**
 * 导出完整社区名单
 *
 * @param data
 */
export const exportFullCommunityList = (
  data: QueryFullListParams
): Promise<AxiosResponse<Blob>> => {
  return request(`/residential/green/residential/exportExcel`, {
    method: 'POST',
    responseType: 'blob',
    data,
  })
}

/**
 * 绿色完整社区下载导出
 *
 * @param greenId
 */
export const downloadGreenInfo = (
  greenId: any
): Promise<AxiosResponse<Blob>> => {
  return request(`/residential/green/community/exportExcel`, {
    method: 'POST',
    responseType: 'blob',
    data: greenId,
  })
}

export type QueryCommunityInfoListParams = {
  communityIds?: string[] //社区id
  streetIds?: string[] //街道id
  regionIds?: string[] //区id
  pageSize: number //页面大小
  pageIndex: number //页数
  startTime: string
  endTime: string
  applyStatus: string
}

/**
 * 分页查询完整社区信息
 *
 * @param data
 */
export const queryCommunityInfoList = (
  data: QueryCommunityInfoListParams
): Promise<Response<PageDataList<any>>> => {
  return request(`/residential/basic/queryCommunity`, {
    method: 'POST',
    data,
  })
}

/**
 * 获取对应年份下，同时是完整社区混合绿色社区的个数
 *
 * @param regionId
 * @param year
 */
export const queryCommunityListNum = (
  regionId: string,
  year: number | string
): Promise<Response<number>> => {
  return request(
    `/residential/green/community/getSame/${regionId || 0}/${year || 2023}`,
    {
      method: 'GET',
    }
  )
}

interface DecideItem {
  communityInfo: '1' | '0' // 社区信息；
  communityManage: '1' | '0' //社区治理信息
  communityName: string // 社区名字
  old: '1' | '0' //养老服务设施
  serviceStation: '1' | '0' //综合服务站
}

/**
 * 获取对应社区对于标准的符合情况
 *
 * @param communityId
 */
export const queryCommunityDecide = (
  communityId: string[]
): Promise<Response<DecideItem[]>> => {
  return request(`/residential/green/community/decide/`, {
    method: 'POST',
    data: communityId,
  })
}

/**
 * 更新社区信息
 *
 * @param type
 * @param communityInfo
 */
export const updateCommunityInfo = (
  type: 0 | 1,
  communityInfo: any
): Promise<Response<any>> => {
  return request(`/proposal/community/base/updateByAll/${type}`, {
    method: 'POST',
    data: communityInfo,
  })
}

/**
 * 获取对应年份
 *
 */
export const queryYear = (): Promise<Response<string[]>> => {
  return request(`/residential/green/residential/getYear`, {
    method: 'GET',
  })
}
/**
 * 查询绿色完整社区汇总表
 *
 */
export const queryFullGreenInfo = (): Promise<AxiosResponse<Blob>> => {
  return request(`/residential/basic/getGreen`, {
    method: 'GET',
    // responseType: "blob",
    // data: greenId,
  })
}
/**
 * 导出绿色完整社区汇总表
 *
 */
export const downloadFullGreenInfo = (): Promise<AxiosResponse<Blob>> => {
  return request(`/residential/basic/exportGreen`, {
    method: 'GET',
    responseType: 'blob',
    // data: greenId,
  })
}
